package com.shouhuyun.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.shouhuyun.dao.MemberDao;
import com.shouhuyun.pojo.Member;
import com.shouhuyun.service.MemberService;
import com.shouhuyun.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 会员服务
 */
@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberDao memberDao;

    // 根据手机号查询 会员是否存在
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    // 添加会员
    public void add(Member member) {
        // 看是否提供了密码
        String password = member.getPassword();
        if (password != null){
            // 使用MD5将明文密码进行加密
            password = MD5Utils.md5(password);
        }
        memberDao.add(member);
    }

    // 根据月份查询会员数量
    public List<Integer> findMemberCountByMonths(List<String> months) {  // 2020.1
        List<Integer> memberCount = new ArrayList<>();
        for (String month : months) {
            String date = month + "31";  // 2020.1.31
            Integer count = memberDao.findMemberCountBeforeDate(date);
            memberCount.add(count);
        }
        return memberCount;
    }

    public List<Integer> findMemberCountByMonth(List<String> months) {  // 2020.1
        List<Integer> memberCount = new ArrayList<>();
        for (int i = 0; i < months.size(); i++) {
            Integer count = memberDao.findMemberCountBeforeDate2(months.get(0),months.get(i));
            memberCount.add(count);
        }
        return memberCount;
    }
}
